package com.avic.rong.adapter.show;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.ArticleDetailActivity;
import com.avic.rong.bean.show_bean.TypeBean;
import com.avic.rong.utils.DateUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

/**
 * 关注
 * Creater: Created by wangyz on 2017/1/10.
 * Email: 936804097@qq.com
 */

public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.RecommendViewHolder> {


    private static final int STYPE_NO_PICTURE = 0;
    private static final int STYPE_ONE_PICTURE = 1;
    private static final int STYPE_THREE_PICTURE = 3;

    private Context mContext;
    private List<TypeBean> data = new ArrayList<>();

    public RecommendAdapter(Context context) {
        this.mContext = context;
    }

    public void setData( List<TypeBean> list){
        this.data = list;
        notifyDataSetChanged();
    }

    @Override
    public RecommendAdapter.RecommendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.view_command, parent, false);
        if (viewType == STYPE_ONE_PICTURE){
            view = LayoutInflater.from(mContext).inflate(R.layout.view_command_one_pic, parent, false);
        }else if(viewType == STYPE_THREE_PICTURE){
            view = LayoutInflater.from(mContext).inflate(R.layout.view_command_three_pic, parent, false);
        }

        return new RecommendViewHolder(view) ;
    }

    @Override
    public void onBindViewHolder(RecommendAdapter.RecommendViewHolder holder,  int position) {
        TypeBean datas = data.get(position);
        String coverImgOne = data.get(position).getCoverImgOne();
        String coverImgtwo = data.get(position).getCoverImgTwo();
        String img = data.get(position).getImg();


        if (coverImgOne != null && coverImgtwo != null) {
            if (!coverImgOne.equals("") && !coverImgtwo.equals("")){
                holder.three_ribao.setText(datas.getAuthor());
                holder.three_before.setText(datas.getPostTime()+"");
                holder.three_readsize.setText(datas.getViewNum()+"阅读");
                holder.three_tv_category_name.setText(datas.getTitle());
                Glide.with(mContext).load(datas.getImg()).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.three_oneimage);
                Glide.with(mContext).load(datas.getCoverImgOne()).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.three_twoimage);
                Glide.with(mContext).load(datas.getCoverImgTwo()).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.three_threeimage);
            }else if (!img.equals("")) {
                holder.one_ridu.setText(datas.getAuthor());
                holder.one_before.setText(datas.getPostTime()+"");
                holder.one_readsize.setText(datas.getViewNum()+"阅读");
                holder.one_tv_category_name.setText(datas.getTitle());
                Glide.with(mContext).load(datas.getImg()).apply(RequestOptions.bitmapTransform(new RoundedCorners(20))).into(holder.one_oneimage);
            } else {
                holder.no_ribao.setText(datas.getAuthor());
                holder.no_before.setText(datas.getPostTime()+"");
                holder.no_readsize.setText(datas.getViewNum()+"阅读");
                holder.no_tv_category_name.setText(datas.getTitle());
            }
        } else if ( img != null) {
            if (!img.equals("")){
                holder.one_ridu.setText(datas.getAuthor());
                holder.one_before.setText(datas.getPostTime()+"");
                holder.one_readsize.setText(datas.getViewNum()+"阅读");
                holder.one_tv_category_name.setText(datas.getTitle());
                Glide.with(mContext).load(datas.getImg()).apply(RequestOptions.bitmapTransform(new RoundedCorners(1))).into(holder.one_oneimage);
            }else {
                holder.no_ribao.setText(datas.getAuthor());
                holder.no_before.setText(datas.getPostTime()+"");
                holder.no_readsize.setText(datas.getViewNum()+"阅读");
                holder.no_tv_category_name.setText(datas.getTitle());
            }
        } else {
            holder.no_ribao.setText(datas.getAuthor());
            holder.no_before.setText(datas.getPostTime()+"");
            holder.no_readsize.setText(datas.getViewNum()+"阅读");
            holder.no_tv_category_name.setText(datas.getTitle());
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int viewNum = data.get(position).getViewNum();
                viewNum++;
                data.get(position).setViewNum(viewNum);
                notifyDataSetChanged();
                Intent intent=new Intent(mContext, ArticleDetailActivity.class);
                int contId = data.get(position).getId();
                intent.putExtra("contId",contId);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        String coverImgOne = data.get(position).getCoverImgOne();
        String coverImgTwo = data.get(position).getCoverImgTwo();
        String img = data.get(position).getImg();

        if (coverImgOne != null && coverImgTwo != null) {
            if (!coverImgOne.equals("") && !coverImgTwo.equals("")) {
                return STYPE_THREE_PICTURE;
            } else if (!img.equals("")) {
                return STYPE_ONE_PICTURE;
            }
            return STYPE_NO_PICTURE;
        } else if (img != null) {
            if (!img.equals("")) {
                return STYPE_ONE_PICTURE;
            }
            return STYPE_NO_PICTURE;
        }
        return STYPE_NO_PICTURE;


    }

    @Override
    public int getItemCount() {
        if (data!=null){
            return data.size();
        }
        return 0;
    }



    class RecommendViewHolder extends RecyclerView.ViewHolder {



        TextView tv_category_name;
        ImageView one_oneimage,three_twoimage,three_oneimage,three_threeimage;
        TextView tv_content;//无图提示
        TextView no_before,no_ribao,no_readsize,no_tv_category_name,one_before,one_tv_category_name,one_toutiao,one_ridu,one_readsize,
                three_before,three_readsize,three_ribao,three_toutiao,three_tv_category_name;

        public RecommendViewHolder(View itemView) {
            super(itemView);

            tv_category_name =itemView.findViewById(R.id.tv_category_name);
            no_before = itemView.findViewById(R.id.no_before);
             no_ribao = itemView.findViewById(R.id.no_ribao);
             no_readsize = itemView.findViewById(R.id.no_readsize);
             no_tv_category_name = itemView.findViewById(R.id.no_tv_category_name);

            one_before = itemView.findViewById(R.id.one_before);
            one_oneimage = itemView.findViewById(R.id.one_oneimage);
            one_readsize = itemView.findViewById(R.id.one_readsize);
            one_ridu = itemView.findViewById(R.id.one_ribao);
          //  one_toutiao = itemView.findViewById(R.id.one_toutiao);
            one_tv_category_name = itemView.findViewById(R.id.one_tv_category_name);

            three_before = itemView.findViewById(R.id.three_before);
            three_oneimage = itemView.findViewById(R.id.three_oneimage);
            three_twoimage = itemView.findViewById(R.id.three_twoimage);
            three_threeimage = itemView.findViewById(R.id.three_threeimage);
            three_readsize = itemView.findViewById(R.id.three_readsize);
            three_ribao = itemView.findViewById(R.id.three_ribao);
          //  three_toutiao = itemView.findViewById(R.id.three_toutiao);
            three_tv_category_name = itemView.findViewById(R.id.three_tv_category_name);

        }
    }
}
